﻿/**
 * Copyright (c) 2013 Jeremy Wyman
 * Microsoft Public License (Ms-PL)
 * This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.
 * 1. Definitions
 *    The terms "reproduce", "reproduction", "derivative works", and "distribution" have the same meaning here as under U.S. copyright law.
 *    A "contribution" is the original software, or any additions or changes to the software.
 *    A "contributor" is any person that distributes its contribution under this license.
 *    "Licensed patents" are a contributor's patent claims that read directly on its contribution.
 * 2. Grant of Rights
 *    (A) Copyright Grant - Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
 *    (B) Patent Grant - Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
 * 3. Conditions and Limitations
 *    (A) No Trademark License - This license does not grant you rights to use any contributors' name, logo, or trademarks.
 *    (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
 *    (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
 *    (D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
 *    (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
 *    
 * More info on: http://tempest.codeplex.com
**/

using System;
using System.Collections;
using System.Collections.Generic;

namespace Tempest.Collections
{
    /// <summary>
    /// Represents a variable size last-in-first-out (LIFO) collection of objects, where the values of the objects may or may not be loaded into memory.
    /// </summary>
    /// <typeparam name="TKey">The type of the keys in the objects.</typeparam>
    /// <typeparam name="TValue">The type of the values in the objects.</typeparam>
    public sealed class LazyStack<TKey, TValue>
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of Isris.LazyStack.
        /// </summary>
        /// <param name="func">The function used to retrieve a TValue for a given TKey.</param>
        public LazyStack(Func<TKey, TValue> func) : this(func, 0) { }
        /// <summary>
        /// Initializes a new instance of Isris.LazyStack.
        /// </summary>
        /// <param name="func">The function used to retrieve a TValue for a given TKey.</param>
        /// <param name="capcacity">The initial number of elements that the dictionary can contain.</param>
        public LazyStack(Func<TKey, TValue> func, int capacity)
        {
            #region Argument Validation
            if (func == null)
                throw new ArgumentNullException("func", "The function pointer to load the value cannot be null");
            #endregion
            _func = func;
            _stack = new Stack<Lazy<TKey, TValue>>(capacity);
        }
        #endregion
        #region Members
        /// <summary>
        /// The number of objects contained in the stack.
        /// </summary>
        public int Count { get { return _stack.Count; } }
        #region Private Members
        private Func<TKey, TValue> _func;
        private Stack<Lazy<TKey, TValue>> _stack;
        #endregion
        #endregion
        #region Methods
        /// <summary>
        /// Returns the key of the object at the top of the stack without removing it.
        /// </summary>
        /// <returns>The key of the object at the top of the stack.</returns>
        public TKey LazyPeek()
        {
            return _stack.Peek().Key;
        }
        /// <summary>
        /// Returns the value of the object at the top of the stack without removing it.
        /// </summary>
        /// <remarks>Calling this function can cause the loading of the object's value into memory.</remarks>
        /// <returns>The value of the object at the top of the stack.</returns>
        public TValue Peek()
        {
            return _stack.Peek().Value;
        }
        /// <summary>
        /// Removes the object at the top of the stack and returns its value.
        /// </summary>
        /// <remarks>Calling this function can cause the loading of the object's value into memory.</remarks>
        /// <returns>The value of the object at the top of the stack.</returns>
        public TValue Pop()
        {
            return _stack.Pop().Value;
        }
        /// <summary>
        /// Pushes an unload object onto the top of stack.
        /// </summary>
        /// <param name="key">A specific key which represents a value.</param>
        public void Push(TKey key)
        {
            Lazy<TKey, TValue> lazy = new Lazy<TKey, TValue>(key, _func);
            _stack.Push(lazy);
        }
        /// <summary>
        /// Adds a loaded object to the top of the stack.
        /// </summary>
        /// <param name="key">A specific key which represents a value.</param>
        /// <param name="value">The value of the element to add. The value can be null for reference types.</param>
        public void Push(TKey key, TValue value)
        {
            Lazy<TKey, TValue> lazy = new Lazy<TKey, TValue>(key, _func);
            lazy.Value = value;
            _stack.Push(lazy);
        }
        /// <summary>
        /// Adds an object to the top of the stack.
        /// </summary>
        /// <param name="item">The object to add to the stack.</param>
        public void Push(Lazy<TKey, TValue> item)
        {
            #region Argument Validation
            if (item == null)
                throw new ArgumentNullException("item", "The Lazy object cannot be null");
            #endregion
            _stack.Push(item);
        }
        #endregion
    }
}
